﻿Imports System.Data.OleDb
Public Class EditProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'if i got to this from this page (ie from pressing a button)
        If Not IsPostBack Then
            Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
            Dim SqlString As String = "SELECT * FROM Profile WHERE TravellerName =@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            oleDbConn.Open()

            'Use DataReader object to get information from Access Database
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            'Check if Database has any records
            If dr.HasRows Then
                'Returns 1 field from database
                dr.Read()
                'Check if value is not null
                If Not IsDBNull(dr("FirstName")) Then
                    tb_FirstName.Text = dr("FirstName")
                End If
                If Not IsDBNull(dr("LastName")) Then
                    tb_LastName.Text = dr("LastName")
                End If
                If Not IsDBNull(dr("Description")) Then
                    tb_Description.Text = dr("Description")
                End If
            End If
        End If
    End Sub

    Protected Sub btn_Save_Click(sender As Object, e As EventArgs) Handles btn_Save.Click
        'Make this more robust - if user uploads a gif or anything else
        Dim myGUID As New System.Guid
        myGUID = Guid.NewGuid()
        Dim newFileName = myGUID.ToString() + ".jpg"
        Dim SqlString As String

        If fup_Picture.HasFile Then
            Dim picLocationOnHardDiskServer = Request.MapPath("Pictures") & "/" & newFileName
            fup_Picture.SaveAs(picLocationOnHardDiskServer)
        End If

        Dim oleDbConn As New OleDb.OleDbConnection(ConfigurationManager.ConnectionStrings("TravelConnectionString").ConnectionString)
        If fup_Picture.HasFile Then
            SqlString = "UPDATE Profile SET FirstName=@f1, LastName=@f2, Description=@f3, Picture=@f4 WHERE TravellerName=@f5"
        Else
            SqlString = "UPDATE Profile SET FirstName=@f1, LastName=@f2, Description=@f3 WHERE TravellerName=@f5"
        End If

        Dim cmd As OleDbCommand = New OleDbCommand(SqlString, oleDbConn)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@f1", tb_FirstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_LastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_Description.Text)
        If fup_Picture.HasFile Then
            cmd.Parameters.AddWithValue("@f4", "Pictures/" & newFileName)
        End If
        cmd.Parameters.AddWithValue("@f5", User.Identity.Name)
        oleDbConn.Open()
        cmd.ExecuteNonQuery()
        'clear all objects created before going to another page
        cmd.Dispose()
        oleDbConn.Close()
        oleDbConn.Dispose()

        'redirect to profile page for the user using query string
        Response.Redirect("Profile.aspx?TravellerName=" & User.Identity.Name)

    End Sub
End Class